<template>
  <div>
     <common-head title="分类页"/>
     <div class="cate-main">
       <div class="cate-left">
         <div :class="['cate',{active:index===num}]" @click="changeCates(index)" v-for="(cate,index) in cates" :key='cate.id'>
           {{cate.name}}
         </div>
       </div>
       <div class="cate-right">
         <div class="zanwu" v-if="!items.length">暂无商品</div>
         <div class="item" v-for="item in items" :key="item.id">
           {{ item.name }}
         </div>
       </div>
     </div>
    <tabbar/>
  </div>
</template>

<script>
import Tabbar from '@components/Tabbar'
import CommonHead from '@components/CommonHead'
import { fetchCates, fetchItems } from '@api'
export default {
  name: 'Cates',
  data () {
    return {
      cates: [],
      num: 0,
      items: []
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    changeCates (index) {
      this.num = index
      this.fetchItems()
    },
    fetchItems () {
      this.items = []
      fetchItems({ categoryId: this.cates[this.num].id }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    },
    fetchCates () {
      fetchCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    }
  },
  components: {
    Tabbar,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.cate-main{
  position: fixed;
  left:0;
  right:0;
  top:46px;
  bottom:50px;
  background: rgb(245, 244, 244);
  display: flex;
  .cate-left{
    width:80px;
    border-right: 1px solid #ccc;
    .cate{
      line-height: 40px;
      font-size: 14px;
      color:#333;
      text-align: center;
      &.active{
        border-left: 2px solid #f12;
        background: rgb(207, 197, 197);
        color: rgb(246, 246, 244);
      }
    }
  }
  .cate-right{
    flex:1;

  }

}
</style>
